﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.QixingManage;

namespace YiSha.Business.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-12-22 11:55
    /// 描 述：业务类
    /// </summary>
    public class ProductVarietyBLL
    {
        private ProductVarietyService productVarietyService = new ProductVarietyService();

        private ProductVarietyVarietyTypeService productVarietyVarietyTypeService = new ProductVarietyVarietyTypeService();

        private GoodsTypesService goodsTypesService = new GoodsTypesService();

        #region 获取数据
        public async Task<TData<List<ProductVarietyEntity>>> GetList(ProductVarietyListParam param)
        {
            TData<List<ProductVarietyEntity>> obj = new TData<List<ProductVarietyEntity>>();
            obj.Data = await productVarietyService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }
        public async Task<TData<List<ProductVarietyEntity>>> GetTenantListJson(TenantProductVarietyListParam param)
        {
            TData<List<ProductVarietyEntity>> obj = new TData<List<ProductVarietyEntity>>();
            obj.Data = await productVarietyService.GetTenantList(param);



            if (param.shopid.IsNullOrZero())
            {
                //obj.Data=obj.Data.Where(t=>t.Id<4).ToList();
                obj.Data = obj.Data.Where(t => t.Id != 3).ToList();
            }
            else
            {
                var shop = goodsTypesService.BaseRepository().FindList<ShopEntity>(t => t.Id == param.shopid).Result.FirstOrDefault();

                if (shop.shop_type.Contains("2") && shop.shop_type.Contains("1") && shop.shop_type.Contains("3"))
                {
                    obj.Data = obj.Data.Where(t =>t.Id != 3).ToList();
                }
                else if (shop.shop_type.Contains("2") && shop.shop_type.Contains("1"))
                {
                    //新车和超市
                    obj.Data = obj.Data.Where(t =>t.Id!=1 && t.Id !=3).ToList();
                }
                else if (shop.shop_type.Contains("3") && shop.shop_type.Contains("1"))
                {
                    //租车和新车
                    obj.Data = obj.Data.Where(t => t.Id== 1 || t.Id == 2).ToList();
                }
                else if (shop.shop_type.Contains("3") && shop.shop_type.Contains("2"))
                {
                    //租车和超市
                    obj.Data = obj.Data.Where(t => t.Id!=2 && t.Id!=3).ToList();
                }
                else if (shop.shop_type.Contains("2"))
                {
                    //超市
                    obj.Data = obj.Data.Where(t => t.Id !=1 && t.Id !=2 && t.Id != 3).ToList();
                }
                else if(shop.shop_type.Contains("3"))
                {
                    //租车
                    obj.Data = obj.Data.Where(t => t.Id==1).ToList();
                }
                else if (shop.shop_type.Contains("1"))
                {
                    //新车
                    obj.Data = obj.Data.Where(t => t.Id==2).ToList();
                }
            }
            

            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }
        

        public async Task<TData<List<ProductVarietyEntity>>> GetPageList(ProductVarietyListParam param, Pagination pagination)
        {
            TData<List<ProductVarietyEntity>> obj = new TData<List<ProductVarietyEntity>>();

            var list=await productVarietyService.GetPageList(param, pagination);

            foreach (var item in list)
            {
                var pvt = productVarietyVarietyTypeService.GetList(new ProductVarietyVarietyTypeListParam() { pvid = item.Id.Value }).Result.Select(t=>t.goodstypeid);

                if (pvt.Count() > 0)
                {
                    var typenames = goodsTypesService.BaseRepository().FindList<GoodsTypesEntity>(t => pvt.Contains(t.Id)).Result.Select(t => t.type_name).ToList();

                    item.goodstypenames = string.Join(',',typenames);
                }
            }
            obj.Data =list;
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<ProductVarietyEntity>> GetEntity(long id)
        {
            TData<ProductVarietyEntity> obj = new TData<ProductVarietyEntity>();

            obj.Data = await productVarietyService.GetEntity(id);
            if (obj.Data != null)
            {
                var pvt = productVarietyVarietyTypeService.GetList(new ProductVarietyVarietyTypeListParam() { pvid =id}).Result.Select(t => t.goodstypeid);

                obj.Data.goodstypeids = string.Join(',', pvt);

                obj.Tag = 1;
            }
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(ProductVarietyEntity entity)
        {
            TData<string> obj = new TData<string>();
            await productVarietyService.SaveForm(entity);

            var pvt=productVarietyVarietyTypeService.GetList(new ProductVarietyVarietyTypeListParam() { pvid = entity.Id.Value }).Result;

            if (pvt.Count>0)
            {
                await productVarietyVarietyTypeService.DeleteForm(string.Join(',', pvt.Select(t => t.Id.Value).ToList()));
            }

            var arrids=TextHelper.SplitToArray<long>(entity.goodstypeids,',');

            foreach (var item in arrids)
            {
                ProductVarietyVarietyTypeEntity product = new ProductVarietyVarietyTypeEntity();
                product.pvid = entity.Id;
                product.goodstypeid = item;
                await productVarietyVarietyTypeService.SaveForm(product);
            }

            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await productVarietyService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        #endregion
    }
}
